﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BulldogCMS.Core.MVC
{
    public class ModuleHelper
    {
        // virtualURL: ~/Content/Site.css
        // output: /BulldogCMS.Modules.TreeRenderer/Content/Site.css
        public string ResolveResourceUrl(string virtualUrl)
        {
            if (Context.Request.CurrentModuleAreaProvider != null)
            {
                virtualUrl = virtualUrl.TrimStart('~');
                string assemblyName = Context.Request.CurrentModuleAreaProvider.CurrentProviderAssembly.GetName().Name;
                string newUrl = VirtualPathUtility.ToAbsolute(string.Format("~/{0}/{1}", assemblyName, virtualUrl));
                return newUrl;
            }
            throw new ApplicationException("You cannot use ModuleHelper outside from BulldogCMS module");
        }

        public string ResolveUrl(string virtualUrl)
        {
            if (virtualUrl == null || !virtualUrl.StartsWith("~/")) throw new ApplicationException("Virtual path must start with ~/");
            if (Context.Request.CurrentModuleAreaProvider != null)
            {
                string newUrl = VirtualPathUtility.ToAbsolute(string.Format("~/{0}{1}", Context.Request.CurrentModuleAreaProvider.AreaName, virtualUrl.TrimStart('~')));
                return newUrl;
            }
            throw new ApplicationException("You cannot use ModuleHelper outside from BulldogCMS module");
        }
    }
}